<template>
    <div>
        <lottie :options="defaultOptions" :height="400" :width="400" v-on:animCreated="handleAnimation"/>
        <!-- <div>
            <p>Speed: x{{animationSpeed}}</p>
            <input type="range" value="1" min="0" max="3" step="0.5"
                   v-on:change="onSpeedChange" v-model="animationSpeed">
        </div> -->
        <!-- <button v-on:click="stop">stop</button>
        <button v-on:click="pause">pause</button>
        <button v-on:click="play">play</button> -->
    </div>
</template>

<script>
  import Lottie from '@/components/lottie/Lottie.vue';
  import animationData from '@/assets/pinjump.json';

  import {login} from '@/services/user'
  import { getMenuRouter } from '@/services/menu.js'
  import {setAuthorization} from '@/utils/request'
  import {loadRoutes} from '@/utils/routerUtil'
  import {mapMutations} from 'vuex'
  import options from '@/router/async/config.fixed.js'

  export default {
    name: 'Loading',
    components: {
      'lottie': Lottie
    },
    data() {
      return {
        defaultOptions: {animationData: animationData},
        animationSpeed: 1,
        permissions: [],
        roles: []
      }
    },
    methods: {
      ...mapMutations('account', ['setUser', 'setPermissions', 'setRoles', 'setToken']),
      handleAnimation: function (anim) {
        this.anim = anim;
      },

      stop: function () {
        this.anim.stop();
      },

      play: function () {
        this.anim.play();
      },

      pause: function () {
        this.anim.pause();
      },

      onSpeedChange: function () {
        this.anim.setSpeed(this.animationSpeed);
      },

      afterLogin(res) {
        this.logging = false
        const loginRes = res.data
        if (loginRes.code >= 0) {
            const {user, permissions, roles, token} = loginRes.data
            this.setUser(user)
            this.setPermissions(permissions)
            this.setRoles(roles)
            this.setToken(token)
            setAuthorization({token: loginRes.data.token, expireAt: new Date(loginRes.data.expireAt)})
            // 获取路由配置
            getMenuRouter().then(res => {
                let { data, state } = res.data.result
                if (state == 200) {
                    data.children = [...options, ...data.children];
                    loadRoutes([data])
                    // this.$router.push('/home')
                    this.$router.push('/home/workplace')
                    this.$message.success(loginRes.message, 3)
                }
                
            })
        } else {
            this.error = loginRes.message
        }
      }
    },
    created () {
        let { username, password, role } = this.$route.query;
        if (!username || !password || !role) {
            this.$message.warning(`登录信息不完整！`).then(() => {
                window.location.href = "//192.168.1.110:8080/platform/login_pt"
            })
            return
        }
        login(username, password, role).then(this.afterLogin)
    }
  }
</script>